﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using Terraria;
using Terraria.Enums;
using Terraria.GameContent;
using Terraria.ID;
using Terraria.Map;
using Terraria.ModLoader;
using Terraria.Net;

namespace MoreEvent.Common.Systems
{
    internal class SunSystem : ModSystem
    {
        public override void Load()
        {
            On_Main.DrawSunAndMoon += Change;
        }
        public override void Unload()
        {
            On_Main.DrawSunAndMoon -= Change;
        }
        private void Change(On_Main.orig_DrawSunAndMoon orig, Main self, Main.SceneArea sceneArea, Color moonColor, Color sunColor, float tempMushroomInfluence)
        {
            Texture2D value = TextureAssets.Sun.Value;
            int num = Main.moonType;
            if (!TextureAssets.Moon.IndexInRange(num))
            {
                num = Utils.Clamp(num, 0, 8);
            }
            Texture2D value2 = TextureAssets.Moon[num].Value;
            int num2 = sceneArea.bgTopY;
            int num3 = (int)(Main.time / 54000.0 * (double)(sceneArea.totalWidth + value.Width * 2)) - value.Width;
            int num4 = 0;
            float num5 = 1f;
            float rotation = (float)(Main.time / 54000.0) * 2f - 7.3f;
            int num6 = (int)(Main.time / 32400.0 * (double)(sceneArea.totalWidth + value2.Width * 2)) - value2.Width;
            int num7 = 0;
            float num8 = 1f;
            float num9 = (float)(Main.time / 32400.0) * 2f - 7.3f;
            if (Main.dayTime)
            {
                double num10;
                if (Main.time < 27000.0)
                {
                    num10 = Math.Pow(1.0 - Main.time / 54000.0 * 2.0, 2.0);
                    num4 = (int)(num2 + num10 * 250.0 + 180.0);
                }
                else
                {
                    num10 = Math.Pow((Main.time / 54000.0 - 0.5) * 2.0, 2.0);
                    num4 = (int)(num2 + num10 * 250.0 + 180.0);
                }
                num5 = (float)(1.2 - num10 * 0.4);
                if(EventSystem.ActiveEvents.Contains(5))
                {
                    num5 *= 4f;
                }
            }
            else
            {
                double num11;
                if (Main.time < 16200.0)
                {
                    num11 = Math.Pow(1.0 - Main.time / 32400.0 * 2.0, 2.0);
                    num7 = (int)(num2 + num11 * 250.0 + 180.0);
                }
                else
                {
                    num11 = Math.Pow((Main.time / 32400.0 - 0.5) * 2.0, 2.0);
                    num7 = (int)(num2 + num11 * 250.0 + 180.0);
                }
                num8 = (float)(1.2 - num11 * 0.4);
            }
            num5 *= Main.ForcedMinimumZoom;
            num8 *= Main.ForcedMinimumZoom;
            if (Main.starGame)
            {
                if (WorldGen.generatingWorld)
                {
                    Main.alreadyGrabbingSunOrMoon = true;
                    if (Main.rand.Next(60) == 0)
                    {
                        for (int i = 0; i < Main.numStars; i++)
                        {
                            if (Main.star[i].hidden)
                            {
                                Star.SpawnStars(i);
                            }
                        }
                    }
                    if (Main.dayTime)
                    {
                        Main.dayTime = false;
                        Main.time = 0.0;
                    }
                }
                else
                {
                    Main.starGame = false;
                }
            }
            else
            {
                Main.starsHit = 0;
            }
            if (Main.dayTime)
            {
                if (Main.remixWorld && !Main.gameMenu || WorldGen.remixWorldGen)
                {
                    return;
                }
                num5 *= 1.1f;
                float num12 = 1f - tempMushroomInfluence;
                num12 -= Main.cloudAlpha * 1.5f * Main.atmo;
                if (num12 < 0f)
                {
                    num12 = 0f;
                }
                Color color = new Color((byte)(255f * num12), (byte)(sunColor.G * num12), (byte)(sunColor.B * num12), (byte)(255f * num12));
                Color color2 = new Color((byte)(sunColor.R * num12), (byte)(sunColor.G * num12), (byte)(sunColor.B * num12), (byte)(sunColor.B * num12));
                bool flag = false;
                if (Main.eclipse)
                {
                    value = TextureAssets.Sun3.Value;
                    flag = true;
                }
                else if (!Main.gameMenu && Main.player[Main.myPlayer].head == 12)
                {
                    value = TextureAssets.Sun2.Value;
                    flag = true;
                }
                if (flag)
                {
                    color2 = new Color((byte)(sunColor.R * num12), (byte)(sunColor.G * num12), (byte)(sunColor.B * num12), (byte)((sunColor.B - 60) * num12));
                }
                Vector2 origin = value.Size() / 2f;
                Vector2 position = new Vector2(num3, num4 + Main.sunModY) + sceneArea.SceneLocalScreenPositionOffset;
                Main.spriteBatch.Draw(value, position, null, color, rotation, origin, num5, SpriteEffects.None, 0f);
                Main.spriteBatch.Draw(value, position, null, color2, rotation, origin, num5, SpriteEffects.None, 0f);
            }
            if (!Main.dayTime)
            {
                float num13 = 1f - Main.cloudAlpha * 1.5f * Main.atmo;
                if (num13 < 0f)
                {
                    num13 = 0f;
                }
                moonColor *= num13;
                Vector2 position2 = new Vector2(num6, num7 + Main.moonModY) + sceneArea.SceneLocalScreenPositionOffset;
                if (WorldGen.drunkWorldGen)
                {
                    Main.spriteBatch.Draw(TextureAssets.SmileyMoon.Value, position2, new Rectangle(0, 0, TextureAssets.SmileyMoon.Width(), TextureAssets.SmileyMoon.Height()), moonColor, num9 / 2f + (float)Math.PI, new Vector2(TextureAssets.SmileyMoon.Width() / 2, TextureAssets.SmileyMoon.Width() / 2), num8, SpriteEffects.None, 0f);
                }
                else if (Main.pumpkinMoon)
                {
                    Main.spriteBatch.Draw(TextureAssets.PumpkinMoon.Value, position2, new Rectangle(0, TextureAssets.PumpkinMoon.Width() * Main.moonPhase, TextureAssets.PumpkinMoon.Width(), TextureAssets.PumpkinMoon.Width()), moonColor, num9, new Vector2(TextureAssets.PumpkinMoon.Width() / 2, TextureAssets.PumpkinMoon.Width() / 2), num8, SpriteEffects.None, 0f);
                }
                else if (Main.snowMoon)
                {
                    Main.spriteBatch.Draw(TextureAssets.SnowMoon.Value, position2, new Rectangle(0, TextureAssets.SnowMoon.Width() * Main.moonPhase, TextureAssets.SnowMoon.Width(), TextureAssets.SnowMoon.Width()), moonColor, num9, new Vector2(TextureAssets.SnowMoon.Width() / 2, TextureAssets.SnowMoon.Width() / 2), num8, SpriteEffects.None, 0f);
                }
                else
                {
                    Main.spriteBatch.Draw(TextureAssets.Moon[num].Value, position2, new Rectangle(0, TextureAssets.Moon[num].Width() * Main.moonPhase, TextureAssets.Moon[num].Width(), TextureAssets.Moon[num].Width()), moonColor, num9, new Vector2(TextureAssets.Moon[num].Width() / 2, TextureAssets.Moon[num].Width() / 2), num8, SpriteEffects.None, 0f);
                }
            }
            Rectangle value3 = !Main.dayTime ? new Rectangle((int)(num6 - TextureAssets.Moon[num].Width() * 0.5 * (double)num8), (int)(num7 - TextureAssets.Moon[num].Width() * 0.5 * (double)num8 + Main.moonModY), (int)(TextureAssets.Moon[num].Width() * num8), (int)(TextureAssets.Moon[num].Width() * num8)) : new Rectangle((int)(num3 - TextureAssets.Sun.Width() * 0.5 * (double)num5), (int)(num4 - TextureAssets.Sun.Height() * 0.5 * (double)num5 + Main.sunModY), (int)(TextureAssets.Sun.Width() * num5), (int)(TextureAssets.Sun.Width() * num5));
            value3.Offset((int)sceneArea.SceneLocalScreenPositionOffset.X, (int)sceneArea.SceneLocalScreenPositionOffset.Y);
            Rectangle rectangle = new(Main.mouseX, Main.mouseY, 1, 1);
            Main.sunModY = (short)(Main.sunModY * 0.999);
            Main.moonModY = (short)(Main.moonModY * 0.999);
            if (!Main.gameMenu || Main.netMode == NetmodeID.MultiplayerClient)
            {
                return;
            }
            if (rectangle.Intersects(value3) || Main.alreadyGrabbingSunOrMoon)
            {
                Main.LocalPlayer.mouseInterface = true;
            }
            if ((Main.mouseLeft || Main.starGame) && Main.hasFocus)
            {
                if (!rectangle.Intersects(value3) && !Main.alreadyGrabbingSunOrMoon)
                {
                    return;
                }
                if (Main.dayTime)
                {
                    Main.time = 54000.0 * (double)((Main.mouseX + TextureAssets.Sun.Width()) / (Main.screenWidth + (float)(TextureAssets.Sun.Width() * 2)));
                    Main.sunModY = (short)(Main.mouseY - num4);
                    if (Main.time > 53990.0)
                    {
                        Main.time = 53990.0;
                    }
                }
                else
                {
                    Main.time = 32400.0 * (double)((Main.mouseX + TextureAssets.Moon[num].Width()) / (Main.screenWidth + (float)(TextureAssets.Moon[num].Width() * 2)));
                    Main.moonModY = (short)(Main.mouseY - num7);
                    if (Main.time > 32390.0)
                    {
                        Main.time = 32390.0;
                    }
                }
                if (Main.time < 10.0)
                {
                    Main.time = 10.0;
                }
                Main.alreadyGrabbingSunOrMoon = true;
            }
            else
            {
                Main.alreadyGrabbingSunOrMoon = false;
            }
        }      
    }
}
